package cn.gxufe.reliable.message.api.holder;

import cn.gxufe.reliable.message.common.Message;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.util.HashMap;

/**
 * @author yingjie.rong
 **/
public class MessageHolder {

    private static Gson gson = new Gson();
    private static ThreadLocal<Message> messageLocal = new ThreadLocal<>();
    private static ThreadLocal<String> messageKeyLocal = new ThreadLocal<>();
    private static ThreadLocal<java.util.Map<String,String>> mapLocal = new ThreadLocal<java.util.Map<String,String>>(){
        @Override
        protected java.util.Map<String,String> initialValue() {
            return new HashMap<>(3);
        }
    };

    public static void setMessageKeyKey(String key){
        messageKeyLocal.set(key);
    }
    public static String getMessageKeyKey(){
       return messageKeyLocal.get();
    }

    public static Message getMessage(){
        return messageLocal.get();
    }

    public static void setMessage(Message message){
        messageLocal.set(message);
    }

    public static java.util.Map<String,String> getMap(){
        return mapLocal.get();
    }

    public static void setValue(String key,String value){
        mapLocal.get().put(key,value);
    }

    public static void setValues(String values){
        java.util.Map<String,String> map = gson.fromJson(values, LinkedTreeMap.class);
        mapLocal.set(map);
    }
}
